package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.server.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 保存用户
     * 参数如果是json格式传递，那么我们接收参数需要添加
     * @return 新增用户的主键
     */
    @PostMapping("/save")
    public ResponseEntity save(@RequestBody User user){
       return userService.save(user);
    }


    /**
     * 根据手机号查找用户
     *
     *
     */
    @GetMapping("/findByMobile")
    public ResponseEntity findByMobile(String mobile){
        int i = 1/0;
        return userService.findByMobile(mobile);
    }



    /**
     *接口名称：登录第一步---手机号登录
     *接口路径：POST/user/login
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map<String,String> paramMap){
        String phone = paramMap.get("phone");
        return userService.sendSms(phone);
    }



    /**
     *接口名称：登录第二步---验证码校验
     *接口路径：POST/user/loginVerification
     *
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> paramMap){
        String phone = paramMap.get("phone");
        String verificationCode = paramMap.get("verificationCode");
        return userService.loginVerification(phone,verificationCode);
    }



    /**
     *接口名称：新用户---1填写资料
     * 接口路径：POST/user/loginReginfo
     *
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo){
        return userService.saveUserInfo(userInfo);
    }


    /**
     *接口名称：新用户---2选取头像
     *接口路径：POST/user/loginReginfo/head
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity updateUserHead(@RequestHeader("Authorization") String token, MultipartFile headPhoto) throws IOException {
        return userService.updateUserHead(headPhoto,token);
    }

}
